package dao.Supper.impl;

import dao.BaseDao;
import dao.Supper.SuperuserDao;
import pojo.Superuser;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 */
public class SuperuserDaoImpl extends BaseDao implements SuperuserDao {

    /**
     * 抽出查询公共信息方法
     *
     * @param sql
     * @param params
     * @return
     */
    @Override
    public Superuser QuerySuer(String sql, Object[] params) {
        Superuser superuser = new Superuser();
        ResultSet rs = this.executeSelect(sql, params);
        try {
            while (rs.next()) {
                superuser.setId(rs.getLong("id"));
                superuser.setUserPassword(rs.getString("userPassword"));
                superuser.setUserLoginName(rs.getString("userLoginName"));
                superuser.setUserImage(rs.getString("userImage"));
                superuser.setUserStatus(rs.getLong("userStatus"));
                superuser.setUserId(rs.getString("userID"));
                superuser.setUserName(rs.getString("userName"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.closeAll();
        }
        return superuser;
    }

    /**
     * 验证用户名和密码
     *
     * @param userName
     * @param userPwd
     * @return
     */
    public Superuser CheckSuser(String userName, String userPwd) {
        String sql = "SELECT * FROM `superuser` where userLoginName=? and userPassword=?";
        Object[] params = {userName, userPwd};
        return QuerySuer(sql, params);
    }

    /**
     * 根据id查询个人信息
     *
     * @param id
     * @return
     */
    public Superuser SelSuser(Long id) {
        String sql = "SELECT * FROM `superuser` WHERE id=?";
        Object[] params = {id};
        return QuerySuer(sql,params);
    }

    /**
     * 修改个人信息
     * @param superuser
     * @return
     */
    public int updateSuser(Superuser superuser) {
        int res = 0;
        try {
            String sql = "UPDATE `superuser` SET  `userName`=?,`userID`=?,`userLoginName`=?  WHERE `id`=?";
            Object[] params = {superuser.getUserName(), superuser.getUserId(), superuser.getUserLoginName(), superuser.getId()};
            res = this.executeUpdateRows(sql, params);
        }finally {
            this.closeAll();
        }
        return res;
    }

    /**
     * 修改密码
     * @param superuser
     * @return
     */
    public int updateSuserPwd(Superuser superuser) {
        int res = 0;
        try {
            String sql = "UPDATE `superuser` SET  `userPassword` =? WHERE `id`=?";
            Object[] params = {superuser.getUserPassword(), superuser.getId()};
            res = this.executeUpdateRows(sql, params);
        }finally {
            this.closeAll();
        }
        return res;
    }


}
